'''
Created on 16.11.2009

@author: adelgald
'''

import sys, os

import wx

import config
import TBP

def parseDate(filehandler, dict):
    try:
        image['date_year']      = image['date_complete'].partition(':')[0]
        image['date_month']     = image['date_complete'].partition(':')[2].partition(':')[0]
        image['date_day']       = image['date_complete'].partition(':')[2].partition(':')[2].partition(' ')[0]
        
        image['time_hour']      = image['date_complete'].partition(' ')[2].partition(':')[0]
        image['time_minute']    = image['date_complete'].partition(' ')[2].partition(':')[2].partition(':')[0]
        image['time_second']    = image['date_complete'].partition(' ')[2].partition(':')[2].partition(':')[2]
        
        return True
    except:
        return False
    

def setExifData(filename, key, value, image):
    try:
        image['handler'].setExifTag(key,value)
        image['handler'].writeFile(filename)
        
        return True
    except:
        return False
    
    
if __name__ == "__main__":
    #main loop
    try:
        config = config.getConfig("local.conf")
    
        if (os.path.isdir(config['source_folder']) == False):
            sys.exit()
    
        if (os.path.isdir(config['target_folder']) == False):
            os.mkdir(config['target_folder'])
    
    
        filelist = ['test.jpg']
        image = {}
    
        for filename in filelist:
            image_file      = TBP.Jpeg(filename)
            image_date      = image_file.getExifTag("DateTimeOriginal")
        
            image['handler']        = image_file
            image['date_complete']  = image_date
        
            parseDate(image['handler'],image)
            setExifData(os.path.join(config['target_folder'],filename), "DateTimeOriginal", "2009:11:12 12:30:00", image)
    except:
        print "Error in __main__ loop"